package com.hh.ListNodeQuestions;

/**
 * @Author hanghao
 * @Date 2023/6/28 11:48
 * @Version 1.0
 * 删除链表的倒数第 N 个结点    (快慢指针系列)
 * middle
 */

public class No19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1, head);
        ListNode fastNode = dummy;
        ListNode slowNode = dummy;
        for (int i = 0; i < n; i++) {
            fastNode = fastNode.next;
        }
//        while (fastNode != null) {
//            fastNode = fastNode.next;
//            slowNode = slowNode.next;
//        }
        //上面这种方式找到的点是那个要被删除的点，假如需要被删除点的前一个位置
        while (fastNode.next != null) {
            fastNode = fastNode.next;
            slowNode = slowNode.next;
        }
        slowNode.next = slowNode.next.next;
        return dummy.next;
    }
}
